package com.niit.controller;

import com.niit.apis.MessageFeignApi;
import com.niit.entity.Message;
import com.niit.entity.MessageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 张月
 * @apiNote
 */
@Controller
@RequestMapping("/api/messages")
public class MessageController {
    @Autowired
    private MessageFeignApi messageFeignApi;
    @GetMapping("/get")
    public ResponseEntity<List<Message>> getChatMessages(
            @RequestParam Integer senderId,
            @RequestParam Integer receiverId) {
        List<Message> messages = messageFeignApi.getChatMessages(senderId, receiverId);
        return ResponseEntity.ok(messages);
    }
    @PostMapping("/send")
    public ResponseEntity<?> sendMessage(@RequestBody Message message) {
        try {
            Message sentMessage = messageFeignApi.sendMessage(message);
            return ResponseEntity.ok(sentMessage);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("发送消息失败");
        }
    }
    @GetMapping("/checkBlockStatus")
    public ResponseEntity<Boolean> checkBlockStatus(
            @RequestParam Integer senderId,
            @RequestParam Integer receiverId) {
        boolean isBlocked = messageFeignApi.checkBlockStatus(senderId, receiverId);
        return ResponseEntity.ok(isBlocked);
    }
    //标记为已读
    @PostMapping("/read/{messageId}")
    public ResponseEntity<Void> markAsRead(@PathVariable Integer messageId) {
        messageFeignApi.markAsRead(messageId);
        return ResponseEntity.ok().build();
    }
    //撤回消息
    @PostMapping("/recall/{messageId}")
    public ResponseEntity<Void> recallMessage(@PathVariable Integer messageId) {
        messageFeignApi.recallMessage(messageId);
        return ResponseEntity.ok().build();
    }
}
